namespace Brjnk.GpxExplorer.Workspace.GCBrowser
{
    using Brjnk.Common;

    /// <summary>
    /// </summary>
    public class GcBrowserTab : BrowserTab
    {
        private GcHtmlReader reader = new GcHtmlReader();

        public GcBrowserTab(Browser browser)
            : base(browser)
        {
            ResetProperties();
        }

        protected override void DocumentLoaded(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.DocumentLoaded(e);
            var result = reader.Read(HTMLDocument);
            if (result.Type == ResultType.Ok)
            {
                bool contains = GcBrowser.ContainsGc(result.Geocache.GcCode);
                CanImport = !contains;
                CanUpdate = contains;
                GcBrowser.AddLoaded(result.Geocache);
            }
            else
            {
                CanImport = false;
                CanUpdate = false;
                Failed = result.Type == ResultType.Failed;
                NotLoggedIn = result.Type == ResultType.NotLoggedIn;
            }
            Result = result;
            Working = false;
        }

        protected override void Navigated(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.Navigated(e);
            ResetProperties();
            Working = true;
        }

        private void ResetProperties()
        {
            Failed = false;
            NotLoggedIn = false;
            CanImport = false;
            CanUpdate = false;
            Working = false;
            Result = null;
        }

        private void ImportOrUpdate()
        {
            GcBrowser.Import(Result.Geocache);

            bool contains = GcBrowser.ContainsGc(Result.Geocache.GcCode);
            CanImport = !contains;
            CanUpdate = contains;
        }

        protected GcBrowser GcBrowser { get { return (GcBrowser)Browser; } }

        private bool canImport;

        public bool CanImport
        {
            get { return canImport; }
            private set { canImport = value; OnPropertyChanged("CanImport"); ImportCommand.IsAvailable = value; }
        }

        private bool canUpdate;

        public bool CanUpdate
        {
            get { return canUpdate; }
            private set { canUpdate = value; OnPropertyChanged("CanUpdate"); UpdateCommand.IsAvailable = value; }
        }

        private bool notLoggedIn;

        public bool NotLoggedIn
        {
            get { return notLoggedIn; }
            private set { notLoggedIn = value; OnPropertyChanged("NotLoggedIn"); }
        }

        private bool failed;

        public bool Failed
        {
            get { return failed; }
            private set { failed = value; OnPropertyChanged("Failed"); }
        }

        private bool working;

        public bool Working
        {
            get { return working; }
            private set { working = value; OnPropertyChanged("Working"); }
        }

        private GcHtmlReaderResult result;

        public GcHtmlReaderResult Result
        {
            get { return result; }
            private set { result = value; OnPropertyChanged("Result"); }
        }

        public GenericCommand ImportCommand { get; private set; }

        public GenericCommand UpdateCommand { get; private set; }

        protected override void CreateCommands()
        {
            base.CreateCommands();
            ImportCommand = new GenericCommand(o => ImportOrUpdate());
            UpdateCommand = new GenericCommand(o => ImportOrUpdate());
        }
    }
}